//
//  CLReweetView.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/13.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit
import SnapKit

class CLReweetView: UIView {
    //记录转发微博的底部约束
    var reweetViewBottom: Constraint?

    //通过viewModel来赋值
    var statusViewModel:CLStatusViewModel?{
        didSet {
            
//            contentLabel.text = statusViewModel?.reweetContent
            contentLabel.attributedText = statusViewModel?.retweetAttributedStr
            //卸载约束
            reweetViewBottom?.uninstall()
            //判断是否有转发微博配图
            if let count = statusViewModel?.status?.reweeted_status?.reposts_count where count > 0 {
                //有配图
                //设置模型
                pictureView.picUrl = statusViewModel?.status?.reweeted_status?.pic_urls
                //设置约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.reweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                    
                })
            pictureView.hidden = false
            }else {
                //没有配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.reweetViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    //懒加载控件
    private lazy var contentLabel:UILabel = {
       let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        //多行显示
        label.numberOfLines = 0
        return label
        
    }()
    private lazy var pictureView:CLStatusPictureView = {
        let collectionView = CLStatusPictureView()
        //设置配图的背景颜色 
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //布局UI
    private func setUpUI(){
    
        contentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(HomeCellMargin)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(HomeCellMargin)
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
